package com.duneyr.snakes.generate;

import java.util.Collection;
import java.util.Random;

import com.duneyr.snakes.Level;

/**
 *
 * @author Daniel
 */
public class LevelGenerator {
    Random random = new Random();
    
    /**
     * Generates a level
     * @param stairsUp
     * A list of the positions of the stairs leading up. If empty the level will
     * be populated with some (one or more) stairs leading up.
     * 
     * @param stairsDown
     * A list of the positions of the stairs leading down. If empty the level will
     * be populated with some (one or more) stairs leading down.
     * @param width the width of the level to be generated
     * @param height the heigth of the level to be generated.
     * @return A newly generated level.
     */
    Level generate(Collection<GenPosition> stairsUp,
            Collection<GenPosition> stairsDown, GenMap map){
        //TODO Make sure there is atleast one way up and one down
        if(stairsUp.isEmpty()){
            //add random... (as long as it doesn't overlap any other)
            
        }
        if(stairsDown.isEmpty()){
            //add random...
            
        }
        generalProcedure(map);
        
        //Decorate the rooms
        map.decorate();
        
        return map.toLevel();
    }

    public void generalProcedure(GenMap map) {
        //add two large rooms
        map.addRandomLargeRoom(random, 10);
        map.addRandomLargeRoom(random, 10);
        
        //add a random number of normal sized rooms
        int n = random.nextInt(10) + 4;
        while(n-->0){
            map.addRandomRoom(random);
        }
        
        //Make some random connections
        n = random.nextInt(4)+2;
        while (n-->0) {
            GenRoom r1 = map.getRandomRoom(random);
            GenRoom r2 = map.getRandomRoom(random);
            map.connect(r1,r2);
        }
        
        //make sure remaining rooms are connected
        GenRoom r1 = map.getRandomRoom(random);
        for(GenRoom r2 : map.rooms()){
            map.connect(r1, r2);
        }
        
        //fill in empty areas next to corridors
        map.roomFill();
    }
    
    
}
